-- Prefan Access Controller
-- Administration Plugin

if not SERVER then return end

require( "gatekeeper" )

function PAC.Kick( owner, ply, reason )
	
	if reason == "nil" then reason = "Kicked by " .. owner:Nick() end
	
	PAC.Print( PAC_CHAT_ALL, Color( 255, 0, 0 ), ply:Nick(), Color( 100, 100, 100 ), " was kicked! [ " .. reason .. " ] " )
	gatekeeper.Drop( ply:UserID(), "KICK: " .. reason )
	
end
PAC.AddChatCommand( "kick", "PLAYER|STRING", "admin", PAC.Kick )

function PAC.MonitorBans( user, pass, steam, ipd )

	user = string.lower( user )

	--PAC.Print( PAC_CONSOLE, user .. " is trying to connect with " .. pass )
	
	local bandata = PAC.LoadBanData( user )
	
	if not bandata then return true end
	
	local len = bandata.banlen
	local at = bandata.banat
	
	if len == 0 then return {false, "You are perma-banned!"} end
	
	local timeleft = string.ToMinutesSeconds( ( len + at ) - os.time() )
	
	if os.time() > len + at then PAC.RemoveData( "pac_data_bans", "player", user ) return true end
	
	if bandata then return {false, "You are banned from this server!  Time left -- " .. timeleft} end
	
	return true
	
end
--hook.Add( "PlayerPasswordAuth", "PAC_MonitorBans", PAC.MonitorBans )

function PAC.Ban( owner, ply, len, reason )

	if not len then len = 0 end
	if not reason then reason = "Banned by " .. owner:Nick() end
	
	PAC.Print( PAC_CHAT_ALL, Color( 255, 0, 0 ), ply:Nick(), Color( 100, 100, 100 ), " was banned! [ " .. reason .. " ] " )

	PAC.SaveBanData( ply, len, reason, owner, os.time() )
	gatekeeper.Drop( ply:UserID(), "BAN: " .. reason )
	
end
PAC.AddChatCommand( "ban", "PLAYER|NUMBER|STRING", "admin", PAC.Ban )

function PAC.UnBan( owner, plynick )

	plynick = string.lower( plynick )

	if not plynick then return end
	if not PAC.DataExists( "pac_data_bans", "player", plynick ) then print( "ERRAR" ) return end
	
	PAC.RemoveData( "pac_data_bans", "player", plynick )
	
end
PAC.AddChatCommand( "unban", "STRING", "admin", PAC.UnBan )

PAC.AddPlugin( "Simple Administration", "Enables Kick and Banning functions in PAC", "Prefanatic" )
